/**   
 * @Title: ScmInvocingProductInforServiceImpl.java 
 * @Package com.kerrykidz.scminvoicing.service.impl 
 * @Description: 与商品操作相关操作的service接口，包括商品，商品分类
 * Copyright: Copyright (c) 2014 
 * Company:wuwh team by iss
 * @author: ScreenJxp   
 * @date: 2014年8月6日 上午11:55:58 
 * @version: V1.0
 * update Release(文件修正记录)
 * <pre>
 * author--updateDate--description----------------------Flag————
 * wuwh    2014-5-1    测试codesyle                      #wuwh001
 *
 *
 *
 * </pre>
 *
 */
package com.kerrykidz.scminvoicing.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.common.dao.mapper.SysAttFileEntityMapper;
import com.kerrykidz.dao.model.GblPosDiscountEntity;
import com.kerrykidz.dao.model.SysAttFileEntity;
import com.kerrykidz.scminvoicing.dao.mapper.GblPosDiscountEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.GblScmPrdCateEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.GblScmPrdClassEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.GblScmPrdDpassEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.GblScmPrdEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.GblScmPrdMemEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.GblScmPrdParEntityMapper;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdCateEntity;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdClassEntity;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdDpassEntity;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdEntity;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdMemEntity;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdParEntity;
import com.kerrykidz.scminvoicing.service.IScmInvocingProductInfoService;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.GenerationUUID;

@Service
public class ScmInvocingProductInfoServiceImpl implements
		IScmInvocingProductInfoService {
	// 商品
	@Autowired
	private GblScmPrdEntityMapper prouductMapper;
	// 商品分类
	@Autowired
	private GblScmPrdCateEntityMapper categoryMapper;
	// 单次票商品
	@Autowired
	private GblScmPrdDpassEntityMapper singelTicketMapper;
	// 会籍商品
	@Autowired
	private GblScmPrdMemEntityMapper memberMapper;
	// 派对团体商品
	@Autowired
	private GblScmPrdParEntityMapper parTeamMapper;
	// 获取商品的折扣信息
	@Autowired
	private GblPosDiscountEntityMapper gblPosDiscountEntityMapper;

	// 保存文件上传的信息
	@Autowired
	private SysAttFileEntityMapper sysAttFileEntityMapper;

	// 课程类商品
	@Autowired
	private GblScmPrdClassEntityMapper classEntityMapper;

	public List<GblScmPrdEntity> findProductList(GblScmPrdEntity entity)
			throws RDPException {
		List<GblScmPrdEntity> resultList = prouductMapper.findProductList(entity);
		return resultList;
	}

	public void saveProduct(GblScmPrdEntity entity) throws RDPException {
		prouductMapper.saveEntity(entity);
	}

	public void updateProduct(GblScmPrdEntity entity) throws RDPException {
		String resverseType = entity.getResverseType();
		// 单次票商品
		if (resverseType != null && "1".equals(resverseType)) {
			GblScmPrdDpassEntity single = new GblScmPrdDpassEntity();
			single.setFkPrdId(entity.getPkPrdId());
			single.setChdCnt(entity.getChdCnt());
			single.setGuaCnt(entity.getGuaCnt());
			singelTicketMapper.updateEntity(single);
		}
		// 会籍商品
		else if (resverseType != null && "2".equals(resverseType)) {
			GblScmPrdMemEntity member = new GblScmPrdMemEntity();
			member.setFkPrdId(entity.getPkPrdId());
			member.setGuaCnt(entity.getGuaCnt());
			member.setMonth(entity.getMonth());
			member.setWeekday(entity.getWeekday());
			memberMapper.updateEntity(member);
		}
		// 派对团体
		else if (resverseType != null && "3".equals(resverseType)
				|| "4".equals(resverseType)) {
			GblScmPrdParEntity parTeam = new GblScmPrdParEntity();
			parTeam.setFkPrdId(entity.getPkPrdId());
			parTeam.setHours(entity.getHours());
			parTeam.setIncFood(entity.getIncFood());
			parTeam.setWeekday(entity.getWeekday());
			parTeamMapper.updateEntity(parTeam);
		}
		// 课程商品
		else if (resverseType != null && "10".equals(resverseType)) {
			GblScmPrdClassEntity classEntity = new GblScmPrdClassEntity();
			classEntity.setFkPrdId(entity.getPkPrdId());
			classEntity.setStartTime(entity.getStartTime());
			classEntity.setEndTime(entity.getEndTime());
			classEntity.setCnt(entity.getCnt());
			classEntityMapper.updateEntity(classEntity);
		}
		prouductMapper.updateEntity(entity);
		// 更该商品的图片信息
	}

	public void deleteProduct(Map<String, Object> map) throws RDPException {
		prouductMapper.deleteEntity(map);
	}

	public List<GblScmPrdCateEntity> findCategoryList(GblScmPrdCateEntity entity)
			throws RDPException {
		return categoryMapper.findCategoryList(entity);
	}

	public void saveProductCate(GblScmPrdCateEntity entity) throws RDPException {
		categoryMapper.saveEntity(entity);
	}

	public void deleteProductCate(GblScmPrdCateEntity entity)
			throws RDPException {
		categoryMapper.deleteCategory(entity);
	}

	public void updateProductCate(GblScmPrdCateEntity entity)
			throws RDPException {
		categoryMapper.updateEntity(entity);
	}

	public void addProduct(GblScmPrdEntity entity) throws RDPException {
		String resverseType = entity.getResverseType();
		// 预处理
		entity.setFkPrdunitId("".equals(entity.getFkPrdunitId()) ? null
				: entity.getFkPrdunitId());
		// entity.setFkPrdunitId(null);
		entity.setFkPicFile("".equals(entity.getFkPicFile()) ? null : entity
				.getFkPicFile());
		if (resverseType != null && "6".equals(resverseType)
				|| "7".equals(resverseType) || "8".equals(resverseType)
				|| "9".equals(resverseType) || "11".equals(resverseType)
				|| "12".equals(resverseType) || "13".equals(resverseType)
				|| "61".equals(resverseType)) {
			// 新增記錄到Att_File
			SysAttFileEntity attFileEntity = new SysAttFileEntity();
			String pkAttfileId = GenerationUUID.getGenerationUUID();
			String picFile = entity.getFkPicFile();
			String filePath = picFile;
			String fileName = picFile.substring(picFile.lastIndexOf("\\") + 1);
			attFileEntity.setPkAttfileId(pkAttfileId);
			attFileEntity.setAttfileName(fileName);
			attFileEntity.setAttfilePath(filePath);
			attFileEntity.setAttType("6");
			attFileEntity.setDelFlag("0");
			attFileEntity.setModiBy(entity.getModiBy());
			attFileEntity.setFkBranId(entity.getFkBranId());
			sysAttFileEntityMapper.insertAttFile(attFileEntity);
			entity.setFkPicFile(pkAttfileId);
		}
		prouductMapper.saveEntity(entity);
		// 单次票商品
		if (resverseType != null && "1".equals(resverseType)) {
			GblScmPrdDpassEntity single = new GblScmPrdDpassEntity();
			single.setFkPrdId(entity.getPkPrdId());
			single.setChdCnt(entity.getChdCnt());
			single.setGuaCnt(entity.getGuaCnt());
			singelTicketMapper.saveEntity(single);
		}
		// 会籍商品
		else if (resverseType != null && "2".equals(resverseType)) {
			GblScmPrdMemEntity member = new GblScmPrdMemEntity();
			member.setFkPrdId(entity.getPkPrdId());
			member.setGuaCnt(entity.getGuaCnt());
			member.setMonth(entity.getMonth());
			member.setWeekday(entity.getWeekday());
			memberMapper.saveEntity(member);
		}
		// 派对团体
		else if (resverseType != null && "3".equals(resverseType)
				|| "4".equals(resverseType)) {
			GblScmPrdParEntity parTeam = new GblScmPrdParEntity();
			parTeam.setFkPrdId(entity.getPkPrdId());
			parTeam.setHours(entity.getHours());
			parTeam.setIncFood(entity.getIncFood());
			parTeam.setWeekday(entity.getWeekday());
			parTeamMapper.saveEntity(parTeam);
		}
		// 课程商品
		else if (resverseType != null && "10".equals(resverseType)) {
			GblScmPrdClassEntity classEntity = new GblScmPrdClassEntity();
			classEntity.setFkPrdId(entity.getPkPrdId());
			classEntity.setStartTime(entity.getStartTime());
			classEntity.setEndTime(entity.getEndTime());
			classEntity.setCnt(entity.getCnt());
			classEntityMapper.saveEntity(classEntity);
		}

		// others wait

	}

	public List<GblScmPrdEntity> getProductInfo(GblScmPrdEntity entity)
			throws RDPException {
		List<GblScmPrdEntity> resultList = prouductMapper
				.getProductInfo(entity);
		return resultList;
	}

	public List<GblScmPrdEntity> getProductListForCollection(String fkPurordId)
			throws RDPException {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("fkPurordId", fkPurordId);
		List<GblScmPrdEntity> resultList = prouductMapper.getProductListForCollection(map);
		return resultList;
	}

	/**
	 * 
	 * 
	 * @Title: findAllDiscount
	 * @Description: <p>
	 *               取得所有预定义折扣列表（经理用）
	 *               <p>
	 * @date: 2014年8月11日
	 * @return: List<GblPosDiscountEntity>
	 * @throws
	 * 
	 */
	public List<GblPosDiscountEntity> findAllDiscount() throws RDPException {
		List<GblPosDiscountEntity> discountList = gblPosDiscountEntityMapper
				.findAllDiscount();
		return discountList;
	}

	public GblScmPrdEntity getProductById(String productId, String branId)
			throws RDPException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("productId", productId);
		map.put("branId", branId);
		List<GblScmPrdEntity> resultList = prouductMapper.getProductById(map);
		if (resultList == null || resultList.size() == 0)
			return null;
		return resultList.get(0);
	}

	public GblScmPrdEntity paginateGoods(GblScmPrdEntity entity)
			throws Exception {
		entity.setIspaging(true);
		entity.setCateCondition(true);
		if (StringUtils.isNotBlank(entity.getFkPrdcateId())) {
			List<GblScmPrdCateEntity> cates = this.recurseCate(entity.getFkPrdcateId(), true, true);
			Set<String> cateids = new HashSet<String>(cates.size());
			for (GblScmPrdCateEntity cate : cates)
				cateids.add(cate.getPkPrdcateId());
			String ids[] = new String[cateids.size()];
			entity.setCateIds(cateids.toArray(ids));
		}
		List<GblScmPrdEntity> result = prouductMapper.getStgProductInfo(entity);
		entity.setResults(result);
		return entity;
	}

	public GblScmPrdEntity getProductList(GblScmPrdEntity entity) throws RDPException {
		List<GblScmPrdEntity> resultList = prouductMapper.getProductInfo(entity);
		entity = resultList == null || resultList.size() == 0 ? null : resultList.get(0);
		return entity;
	}

	public void saveClassProduct(GblScmPrdClassEntity entity)
			throws RDPException {
		classEntityMapper.saveEntity(entity);
	}

	public GblScmPrdEntity findProductById(GblScmPrdEntity entity)
			throws RDPException {
		String resverseType = entity.getResverseType();
		if (resverseType != null && "1".equals(resverseType)) {
			entity = prouductMapper.findSingleTicket(entity);
		} else if (resverseType != null && "2".equals(resverseType)) {
			entity = prouductMapper.findMemberProduct(entity);
		} else if (resverseType != null && "3".equals(resverseType)
				|| "4".equals(resverseType)) {
			entity = prouductMapper.findParOrTeam(entity);
		} else if (resverseType != null && "10".equals(resverseType)) {
			entity = prouductMapper.findLessonsProduct(entity);
		}// 普通商品
		else {
			entity = prouductMapper.findProductById(entity);
		}
		return entity;
	}

	public List<String> findChildCateById(String cateId) throws RDPException {
		return categoryMapper.findChildCateById(cateId);
	}

	/**
	 * 根据ID查询关联单次票扩展信息
	 * 
	 * @param pkId
	 * @return
	 * @throws RDPException
	 * @date: 2014年8月28日
	 */
	public GblScmPrdEntity findSingleTicket(String pkId) throws RDPException {
		GblScmPrdEntity prd = new GblScmPrdEntity();
		prd.setPkPrdId(pkId);
		return prouductMapper.findSingleTicket(prd);
	}

	/**
	 * 根据会员查询单次票商品
	 * 
	 * @return
	 */
	public List<GblScmPrdEntity> findSingleTicketByMemId(
			@Param("pkMemId") String pkMemId,
			@Param("resverseType") String resverseType) {
		return prouductMapper.findSingleTicketByMemId(pkMemId, resverseType);
	}

	/**
	 * 查询所有课程类商品
	 * 
	 * @return
	 */
	public List<GblScmPrdEntity> findAllLessonsProduct() {
		return prouductMapper.findAllLessonsProduct();
	}

	public Integer deleteAbled(String cateId) throws RDPException {
		return categoryMapper.deleteAbled(cateId);
	}

	public String canAbled(GblScmPrdEntity entity) throws RDPException {
		List<String> list = prouductMapper.canAbled(entity);
		int unComplet = 0;
		int Completed = 0;
		if (list == null || list.size() == 0) {
			return "0";// 可以删除
		} else {
			String[] str;
			for (String arr : list) {
				str = arr.split("_");
				if ("2".equals(str[1]) || "4".equals(str[1])
						|| "1".equals(str[1])) {
					unComplet += Integer.parseInt(str[2]);
				} else if ("3".equals(str[1])) {
					Completed = Integer.parseInt(str[2]);
				}
			}
			if (Completed > 0) {
				return "1";// 不可删除
			}
			if (unComplet > 0) {
				return "2";// 删除给出提示
			}
		}
		return "0";
	}

	@Override
	public List<GblScmPrdCateEntity> recurseCate(String id, boolean hasCurrent,
			boolean lower) throws RDPException {
		List<GblScmPrdCateEntity> result = new ArrayList<GblScmPrdCateEntity>();
		if (lower) {
			if (hasCurrent) {
				GblScmPrdCateEntity cate = new GblScmPrdCateEntity();
				cate.setPkPrdcateId(id == "" ? null : id);
				cate.setPkSelect(true);
				cate = this.categoryMapper.findEntity(cate);
				if (cate != null)
					result.add(cate);
			}
			recurseLower(result, id);
		} else
			recurseHigher(result, id);
		return result;
	}

	private void recurseLower(List<GblScmPrdCateEntity> result, String id)
			throws RDPException {
		GblScmPrdCateEntity cate = new GblScmPrdCateEntity();
		cate.setFkParPrdcateId(id == "" ? null : id);
		cate.setParPkSelect(true);
		List<GblScmPrdCateEntity> cates = categoryMapper.findCategoryList(cate);
		for (GblScmPrdCateEntity cat : cates) {
			result.add(cat);
			recurseLower(result, cat.getPkPrdcateId());
		}
	}

	private void recurseHigher(List<GblScmPrdCateEntity> result, String id)
			throws RDPException {
		GblScmPrdCateEntity cate = new GblScmPrdCateEntity();
		if (id != null) {
			cate.setPkPrdcateId(id);
			cate.setPkSelect(true);
		} else {
			cate.setFkParPrdcateId(id);
			cate.setParPkSelect(true);
		}

		cate = this.categoryMapper.findEntity(cate);
		if (cate != null) {
			if (StringUtils.isNotBlank(cate.getFkParPrdcateId()))
				recurseHigher(result, cate.getFkParPrdcateId());
			result.add(cate);
		}
	}

}
